﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DMS.Business.WebControls;

namespace DMS.Business.Utility
{
    public class FormBinder
    {

        public static void BindObjectToControls(object obj, Control container)
        {
            if (obj == null) return;
            Type objType = obj.GetType();
            PropertyInfo[] objPropertiesArray = objType.GetProperties();
            foreach (PropertyInfo objProperty in objPropertiesArray)
            {
                Control control = container.FindControl(objProperty.Name);
                if(control == null)
                    continue;
                bool success = false;
                bool isCustom = false;
                if (control is YiduListBox)
                {
                    YiduListBox listBox = (YiduListBox)control;
                    string propertyValue = objProperty.GetValue(obj, null).ToString();
                    listBox.SetListValue(propertyValue);
                    isCustom = true;
                }
                if (control is YiduCheckBoxList)
                {
                    YiduCheckBoxList checkBoxList = (YiduCheckBoxList)control;
                    string propertyValue = objProperty.GetValue(obj, null).ToString();
                    checkBoxList.SetListValue(propertyValue);
                    isCustom = true;
                }
                if (!isCustom)
                {
                    if (control is ListControl)
                    {
                        ListControl listControl = (ListControl)control;
                        var objValue = objProperty.GetValue(obj, null);
                        if(objValue==null)
                            continue;
                        string propertyValue = objValue.ToString();
                        ListItem listItem = listControl.Items.FindByValue(propertyValue);
                        if (listItem != null) listItem.Selected = true;
                    }
                    else
                    {
                        Type controlType = control.GetType();
                        PropertyInfo[] controlPropertiesArray = controlType.GetProperties();
                        success = FindAndSetControlProperty(obj, objProperty, control, controlPropertiesArray, "Checked", typeof(bool));
                        if (!success)
                        {
                            success = FindAndSetControlProperty(obj, objProperty, control, controlPropertiesArray, "SelectedDate", typeof(DateTime));
                            if (success)
                                FindAndSetControlProperty(obj, objProperty, control, controlPropertiesArray, "VisibleDate", typeof(DateTime));
                        }
                        if (!success)
                            success = FindAndSetControlProperty(obj, objProperty, control, controlPropertiesArray, "Value", typeof(String));
                        if (!success)
                            success = FindAndSetControlProperty(obj, objProperty, control, controlPropertiesArray, "Text", typeof(String));
                    }
                }
            }
        }
        private static bool FindAndSetControlProperty(object obj, PropertyInfo objProperty, Control control, PropertyInfo[] controlPropertiesArray, string propertyName, Type type)
        {
            foreach (PropertyInfo controlProperty in controlPropertiesArray)
            {
                if (controlProperty.Name == propertyName && controlProperty.PropertyType == type)
                {
                    controlProperty.SetValue(control, objProperty.GetValue(obj, null).ChangeType(type), null);
                    return true;
                }
            }
            return false;
        }
        public static void BindControlsToObject(object obj, Control container)
        {
            if (obj == null) return;
            Type objType = obj.GetType();
            PropertyInfo[] objPropertiesArray = objType.GetProperties();
            foreach (PropertyInfo objProperty in objPropertiesArray)
            {
                bool success = false;
                bool isCustom = false;
                Control control = container.FindControl(objProperty.Name);

                if (control == null)
                    continue;
                 

                if (control is YiduListBox)
                {
                    YiduListBox listBox = (YiduListBox)control;
                    if (listBox.SelectedIndex > -1)
                        objProperty.SetValue(obj, listBox.GetListValue().ChangeType(objProperty.PropertyType), null);
                    isCustom = true;
                }
                if (control is YiduCheckBoxList)
                {
                    YiduCheckBoxList checkBoxList = (YiduCheckBoxList)control;
                    if (checkBoxList.SelectedIndex > -1)
                        objProperty.SetValue(obj, checkBoxList.GetListValue().ChangeType(objProperty.PropertyType), null);
                    isCustom = true;
                }
                if (!isCustom)
                {
                    if (control is ListControl)
                    {
                        ListControl listControl = (ListControl)control;
                        if (listControl.SelectedItem != null)
                        {
                            objProperty.SetValue(obj, listControl.SelectedItem.Value.ChangeType(objProperty.PropertyType), null);
                        }
                    }
                    else
                    {
                        //TODO 日期 label暂时无法获取
                        Type controlType = control.GetType();
                        PropertyInfo[] controlPropertiesArray = controlType.GetProperties();
                        success = FindAndGetControlProperty(obj, objProperty, control, controlPropertiesArray, "Checked", typeof(bool));
                        if (!success)
                            success = FindAndGetControlProperty(obj, objProperty, control, controlPropertiesArray, "SelectedDate", typeof(DateTime));
                        if (!success)
                            success = FindAndGetControlProperty(obj, objProperty, control, controlPropertiesArray, "Value", typeof(String));
                        if (!success)
                            success = FindAndGetControlProperty(obj, objProperty, control, controlPropertiesArray, "Text", typeof(String));
                    }
                }
            }
        }
        private static bool FindAndGetControlProperty(object obj, PropertyInfo objProperty, Control control, PropertyInfo[] controlPropertiesArray, string propertyName, Type type)
        {
            foreach (PropertyInfo controlProperty in controlPropertiesArray)
            {
                if (controlProperty.Name == propertyName && controlProperty.PropertyType == type)
                {
                    try
                    {
                        object typeObj = controlProperty.GetValue(control, null).ChangeType(objProperty.PropertyType);
                        objProperty.SetValue(obj, typeObj, null);
                        return true;
                    }
                    catch
                    {
                        return false;
                    }
                }
            }
            return false;
        }
    }
}
